var http = require("http");
var url = require("url");
var sleep = require("./sleep");
var port = 9999;

function start(route){
    function requestHandler(req, res){
        setImmediate(function(){
            sleep(5000);
            var pathname = url.parse(req.url).pathname;
            console.log("Request for: '" + pathname + "' recieved.");
            route(pathname);

            res.writeHead(200, {"Content-Type" : "text/html"});
            res.write("<html><body><h1>Hello Node.js.</h1></body></html>");
            res.end();
        });
    }

    http.createServer(requestHandler).listen(port);
    console.log("server running: 127.0.0.1:" + port);
}

exports.start = start;